package com.zs.zcerp.entrance.service.impl;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.zcerp.entrance.entity.EpMain;
import com.zs.zcerp.entrance.mapper.EpMainMapper;
import com.zs.zcerp.entrance.service.IEpMainService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import java.util.*;

@DS("oracle2")
@Service
public class EpMainServiceImpl extends ServiceImpl<EpMainMapper, EpMain> implements IEpMainService {

    @Override
    public List<EpMain> listEpMains() {
        return baseMapper.selectList(new QueryWrapper<EpMain>().ne("status", "0").and(wrapper -> wrapper.gt("OUT_DATE", new Date()).or().isNull("OUT_DATE")));
    }

    @Override
    public IPage<EpMain> pageListEpMainsLately(Page page) {
        return baseMapper.selectPage(page, new QueryWrapper<EpMain>().gt("LAST_DATE", DateUtils.addDays(new Date(), -15)));
    }

    @Override
    public List<EpMain> listEpMainsAttendance(Integer orgId) {
        return baseMapper.selectList(new QueryWrapper<EpMain>().ne("status", "0").eq("ORG_ID", orgId).and(wrapper -> wrapper.between("JOB_LEVEL", '1', '8').or().isNull("JOB_LEVEL")).and(wrapper -> wrapper.gt("OUT_DATE", new Date()).or().isNull("OUT_DATE")));
    }

    @Override
    public List<EpMain> listEpMainsLately() {
        return baseMapper.selectList(new QueryWrapper<EpMain>().gt("LAST_DATE", DateUtils.addDays(new Date(), -1)));
    }

    @Override
    public EpMain findByEmp_No(String empNo) {
        return baseMapper.selectOne(new QueryWrapper<EpMain>().eq("EMP_NO", empNo));
    }

    @Override
    public EpMain findByEmpno(String empNo) {
        return baseMapper.selectOne(new QueryWrapper<EpMain>().eq("EMPNO", empNo));
    }

    @Override
    public EpMain findByEmpnoActive(String empNo) {
        return baseMapper.selectOne(new QueryWrapper<EpMain>().eq("EMPNO", empNo).ne("status", "0").and(wrapper -> wrapper.gt("OUT_DATE", new Date()).or().isNull("OUT_DATE")));
    }

    @Override
    public int updateEmp(EpMain epMain) {
        return baseMapper.update(epMain, new QueryWrapper<EpMain>().eq("EMP_NO", epMain.getEmpCode()));
    }

    @Override
    public List<EpMain> ListOutEmpAll() {
        return baseMapper.selectList(new QueryWrapper<EpMain>().lt("OUT_DATE", new Date()));
    }

    @Override
    public List<EpMain> ListOutEmpAllNow() {
        return baseMapper.selectList(new QueryWrapper<EpMain>().lt("OUT_DATE", new Date()));
    }

    @Override
    public List<EpMain> listAllEmp() {
        return baseMapper.selectList(new QueryWrapper<EpMain>());
    }

    @Override
    public List<EpMain> listAllCancellationEmp() {
        return baseMapper.selectList(new QueryWrapper<EpMain>().eq("status", 0));
    }

    @Override
    public EpMain findByEmpnoAndOrgid(String empNo, Integer orgId) {
        return baseMapper.selectOne(new QueryWrapper<EpMain>().eq("EMPNO", empNo).eq("ORG_ID", orgId));
    }
}
